package com.comunicacion2;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.comunicacion2.jpingy.Ping;
import com.comunicacion2.jpingy.Ping.Backend;
import com.comunicacion2.jpingy.PingArguments;
import com.comunicacion2.jpingy.PingResult;

public class DatosRed {

	int level;
	int ip;

	public DatosRed(int level, int ip) {
		super();
		this.level = level;
		this.ip = ip;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getIp() {
		return ip;
	}

	public void setIp(int ip) {
		this.ip = ip;
	}

	private String getStringIp(int ip) {
		byte[] bytes = BigInteger.valueOf(ip).toByteArray();
		InetAddress address = null;
		try {
			address = InetAddress.getByAddress(bytes);
			String textIp = address.getHostAddress();
			return textIp;
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}

	// Retorna tiempo si se configuro la ip (distinta de cero -> estaba
	// conectado).
	public String getTiempoViajePaq() {
		String tiempo = "NaN";
		if (ip != 0) {
			// PingArguments arguments = new
			// PingArguments.Builder().url(this.getStringIp(ip)).timeout(5000).count(2).bytes(32).build();
			PingArguments arguments = new PingArguments.Builder()
					.url("google.com").timeout(5000).count(2).bytes(32).build();
			PingResult results = Ping.ping(arguments, Backend.UNIX);
			tiempo = String.valueOf(results.rtt_min());
		}
		return tiempo;
	}

}
